//
//  ViewController.m
//  GCD_Demo_OC
//
//  Created by zhaoguoteng on 2020/5/4.
//  Copyright © 2020 zhaoguoteng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic, strong) dispatch_queue_t customQ;

@property(nonatomic, assign) NSInteger foo;
@property(nonatomic, assign) NSInteger bar;

@property(nonatomic, strong) dispatch_source_t source;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    dispatch_group_t group = dispatch_group_create();

    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_group_async(group, queue, ^(){
        // Do something that takes a while
//        [self doSomeFoo];
        dispatch_group_async(group, dispatch_get_main_queue(), ^(){
            self.foo = 42;
        });
    });
    dispatch_group_async(group, queue, ^(){
        // Do something else that takes a while
//        [self doSomeBar];
        dispatch_group_async(group, dispatch_get_main_queue(), ^(){
            self.bar = 1;
        });
    });

    // This block will run once everything above is done:
    dispatch_group_notify(group, dispatch_get_main_queue(), ^(){
        NSLog(@"foo: %d", self.foo);
        NSLog(@"bar: %d", self.bar);
    });
    
//    NSRunningApplication *mail = [NSRunningApplication
//      runningApplicationsWithBundleIdentifier:@"com.apple.mail"];
//    if (mail == nil) {
//        return;
//    }
//    pid_t const pid = mail.processIdentifier;
//    self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid,
//      DISPATCH_PROC_EXIT, DISPATCH_TARGET_QUEUE_DEFAULT);
//    dispatch_source_set_event_handler(self.source, ^(){
//        NSLog(@"Mail quit.");
//    });
//    dispatch_resume(self.source);
    
//    size_t const objectCount = 1000;
//    uint64_t n = dispatch_benchmark(10000, ^{
//        @autoreleasepool {
//            id obj = @42;
//            NSMutableArray *array = [NSMutableArray array];
//            for (size_t i = 0; i < objectCount; ++i) {
//                [array addObject:obj];
//            }
//        }
//    });
//    NSLog(@"-[NSMutableArray addObject:] : %llu ns", n);
}

@end
